package com.yuejie.music.manager.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.yuejie.db.DBConnection;
import com.yuejie.music.manager.MusicScoreManager;

/**
 * @ClassName: MusicScoreManagerImpl
 * @Description: 评分处理
 * @author husongbo
 * @date 2012-7-2 下午05:09:39
 * 
 */

public class MusicScoreManagerImpl implements MusicScoreManager{
	
	DBConnection db = null;
	
	public MusicScoreManagerImpl()
	{
		db = new DBConnection();
	}
	
	public String updateMusicScore(String id, int score) throws Exception 
	{
		String result = "失败";
		Object[] params = null;
		List<Object[]> paramList = new ArrayList<Object[]>();
			
		String strSQL = "";
		if(judgeMusicInfo(id))
		{
			strSQL = UPDATE_SQL;
			params = new Object[]{score, id};
		}
		else
		{
			strSQL = INSERT_SQL;
			params = new Object[]{id, score};
		}
		paramList.add(params);
		boolean flag = db.insertSMSInfo(strSQL, paramList);
		
		db.close();
		
		if(flag)
		{
			result = "成功";
		}
		
		return result;
	}
	
	public boolean judgeMusicInfo(String id) throws Exception
	{
		Object[] params = new Object[]{id};
		List<Map<String, Object>> list = db.querySMSInfo(SELECT_BY_ID_SQL, params);
		
		if(list != null && list.size() != 0) 
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public String getMusicInfo(String id) throws Exception
	{
		String result = "";
		Object[] params = new Object[]{id};
		List<Map<String, Object>> list = db.querySMSInfo(SELECT_BY_ID_SQL, params);
		
		if(list != null && list.size() != 0) 
		{
			Map<String, Object> map = list.get(0);
			result = String.valueOf(map.get("music_score"));
		}
		else
		{
			result = "没有记录";
		}
		db.close();
		
		return result;
	}
	
	public final static String UPDATE_SQL = "UPDATE SCORE SET MUSIC_SCORE = ? WHERE MUSIC_ID = ?";
	
	public final static String INSERT_SQL = "INSERT INTO SCORE(MUSIC_ID, MUSIC_SCORE) VALUES(?, ?)";
	
	public final static String SELECT_BY_ID_SQL = "SELECT MUSIC_ID, MUSIC_SCORE FROM SCORE WHERE MUSIC_ID = ?";

}
